import requests
from bs4 import BeautifulSoup

# 最简单的爬虫demo
def simple_crawler(url):
    try:
        # 发送HTTP GET请求
        response = requests.get(url)
        # 设置响应的编码格式为UTF-8
        response.encoding = 'utf-8'
        # 检查请求是否成功
        response.raise_for_status()
    except requests.exceptions.RequestException as e:
        print(f"请求错误: {e}")
        return

    # 解析HTML内容
    soup = BeautifulSoup(response.text, 'html.parser')

    # 打印整个网页的标题
    title = soup.title.string if soup.title else '无标题'
    print(f"网页标题: {title}")

    # 示例：提取所有的段落文本
    paragraphs = soup.find_all('p')
    print("\n网页中的段落内容:")
    for idx, para in enumerate(paragraphs, start=1):
        print(f"{idx}. {para.get_text()}")

if __name__ == "__main__":
    url = input("请输入要爬取的网页URL: ").strip()
    simple_crawler(url)

